package com.qingxin.sys.verifier.rules;

import java.util.HashMap;
import java.util.Map;

import com.qingxin.sys.verifier.FormMap;
import com.qingxin.sys.verifier.VerifyRule;

/**
 * Ajax验证规则
 * @author Administrator
 *
 */
public class AjaxRule extends VerifyRule {
	
	private String type;
	private String dataType;
	private boolean async;
	private String url;
	private String params;
	private String okcode;
	private String errcode;
	
	
	public AjaxRule() {
		this.type = "GET";
		this.dataType = "text";
		this.async = true;
		this.okcode = "0";
		this.errcode = "1";
		setBackground(false);//该类型不进行后台验证
	}
	
	
	@Override
	public boolean isValid(String name, String label, FormMap formMap) {
		return true;//该类型不进行后台验证
	}
	
	
	@Override
	public Map<String, Object> getRuleCfg(String name, String label) {
		if(isEmpty(url)){
			return null;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("rtype", "ajax");
		if(!"GET".equalsIgnoreCase(type)){
			map.put("type", type);
		}
		if(!"text".equalsIgnoreCase(dataType)){
			map.put("dataType", dataType);
		}
		if(!async){
			map.put("async", async);
		}
		map.put("url", url);
		map.put("params", params);
		map.put("okcode", okcode);
		map.put("errcode", errcode);
		
		String errormsg = createMessage(null, "{name}", label, "{label}", label);
		if(!isEmpty(errormsg)){
			map.put("error", errormsg);
		}
		
		return map;
	}
	
	
	@Override
	protected void handleRuleArg(String arg) {
		super.handleRuleArg(arg);
		if("g".equals(arg)){
			setType("GET");
		}else if("p".equals(arg)){
			setType("POST");
		}else if("a".equals(arg)){
			setAsync(true);
		}else if("A".equals(arg)){
			setAsync(false);
		}else if("t".equals(arg)){
			setDataType("text");
		}else if("j".equals(arg)){
			setDataType("json");
		}
	}
	
	
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getDataType() {
		return dataType;
	}
	public void setDataType(String dataType) {
		this.dataType = dataType;
	}
	public boolean isAsync() {
		return async;
	}
	public void setAsync(boolean async) {
		this.async = async;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getParams() {
		return params;
	}
	public void setParams(String params) {
		this.params = params;
	}
	public String getOkcode() {
		return okcode;
	}
	public void setOkcode(String okcode) {
		this.okcode = okcode;
	}
	public String getErrcode() {
		return errcode;
	}
	public void setErrcode(String errcode) {
		this.errcode = errcode;
	}
	
	
	@Override
	public void setForeground(boolean foreground) {
		super.setForeground(false);
	}
	
}
